package activity

import (
	"fmt"
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/activity"
	"gq_volunteer_platform/model/user"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	var req struct {
		Page      int    `binding:"required,gte=1,max=10000000"`
		Limit     int    `binding:"required,gte=1,max=10000000"`
		ModelType string `binding:"oneof=活动 培训"`
		Name      string `binding:"max=255"`
		Status    string `binding:"oneof='' 招募中 进行中 已结束"`
		Type      string `binding:"max=255"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	var count int64
	var list []struct {
		ID                       uint
		Name                     string
		Type                     string
		Status                   string
		RegistrationDeadlineTime string
		RecruitmentNumber        uint
	}

	var tx = bean.MysqlDb.Model(&activity.Activity{}).Where("`status` in ('招募中','进行中','已结束')")

	tx = tx.Where("`model_type`=?", req.ModelType)

	if req.Name != "" {
		tx = tx.Where("`name` like ?", "%"+req.Name+"%")
	}

	if req.Status != "" {
		tx = tx.Where("`status`=?", req.Status)
	}

	if req.Type != "" {
		tx = tx.Where("`type`=?", req.Type)
	}

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Order("`id` DESC").Limit(req.Limit).Offset(offset).Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})

}

func Detail(c *gin.Context) {

	var req struct {
		ID uint `binding:"max=10000000"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	// 获取登录用户对象
	var u user.User
	if err := u.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	var tx = bean.MysqlDb.Model(&activity.Activity{}).Where("`status` in ('招募中','进行中','已结束')")

	tx = tx.Where("id=?", req.ID)

	var one activity.Activity

	if err := tx.First(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("活动不存在id:%d", req.ID))
		return
	}

	// userList
	var userList []struct {
		ID          uint
		Name        string
		CreatedAt   string
		CheckStatus string
		SignStatus  string
	}

	if err := bean.MysqlDb.Model(&activity.ActivityUser{}).
		Select("`activity_user`.`user_id` as id,`user`.`real_name` as name,`activity_user`.`created_at`,`activity_user`.`check_status`,`activity_user`.`sign_status`").
		Joins("LEFT JOIN `user` on `user`.`id` = `activity_user`.`user_id`").
		Where("`activity_user`.`activity_id`=?", one.ID).
		Where("`activity_user`.`check_status`=?", "已审核").Find(&userList).Error; err != nil {

		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("活动不存在id:%d", req.ID))
		return
	}

	// 查询自己在活动中的 审核状态
	var myActivityStatus = ""
	var myActivityUser activity.ActivityUser
	if err := bean.MysqlDb.Where("`activity_id`=? AND `user_id`=?", one.ID, u.ID).Find(&myActivityUser).Error; err != nil {
		bean.Log.Warning(err)
		c.String(500, "发生错误")
		return
	}
	myActivityStatus = myActivityUser.CheckStatus

	var inUserList bool
	for _, item := range userList {
		if item.ID == u.ID {
			inUserList = true
			break
		}
	}

	var is_responsible_person bool
	if one.ResponsibleUserId == u.ID {
		is_responsible_person = true
	}

	c.PureJSON(200, gin.H{
		"detail":                one,
		"userList":              userList,
		"inUserList":            inUserList,
		"myActivityStatus":      myActivityStatus,
		"is_responsible_person": is_responsible_person,
	})
}
